package com.example.administrator.szb.util;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AlertDialog;

/**
 * Created by stone on 2018/2/8.
 */

public class NotificationMUtil {
    public static boolean isOpened(final Context context){



        NotificationManagerCompat manager = NotificationManagerCompat.from(context);
        boolean isOpened = manager.areNotificationsEnabled();
        if (isOpened){
            QTLog.e("已打开通知权限");
        }else {
            QTLog.e("未打开通知权限");
        }
        if (!isOpened){
            new AlertDialog.Builder(context)
                    .setTitle("提示")
                    .setMessage("检测到您未打开通知权限，打开通知权限，不错过重要的消息呦！")
                    .setCancelable(false)
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            SPUtils.setDatas("notifyChecked","checked");
                        }
                    })
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            SPUtils.setDatas("notifyChecked","checked");
                            goOpen(context);
                        }
                    })
                    .show();
        }
        return isOpened;
    }


    public static void goOpen(Context context){
        // 根据isOpened结果，判断是否需要提醒用户跳转AppInfo页面，去打开App通知权限
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", context.getPackageName(), null);
        intent.setData(uri);
        context.startActivity(intent);
    }
}
